package com.hotel.hotelsystem.controller;

import com.hotel.hotelsystem.pojo.HotelList;
import com.hotel.hotelsystem.pojo.HotelListES;
import com.hotel.hotelsystem.service.EsHotelListService;
import com.hotel.hotelsystem.service.HotelListService;
import com.hotel.hotelsystem.service.NoEsHotelListService;
import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;

/**
 * 主页搜索框 elasticSearch 按照酒店名称进行搜索
 */
@RestController
@RequestMapping("/hotelList")
public class ElasticSearchController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;

    @Autowired
    private EsHotelListService esHotelListService;

    @Autowired
    private HotelListService hotelListService;

    @Autowired
    private NoEsHotelListService noEsHotelListService;


    /**
     * mysql中酒店筛选数据存放到elasticSearch当中
     * 1.查询出mysql酒店筛选所有数据
     * @return
     */
    @GetMapping("/mysqlSynchroElasticSearch")
    public String mysqlSynchroElasticSearch(){
        List<HotelList> hotelLists = hotelListService.list();
        System.out.println("长度:"+hotelLists.size());
        ArrayList<HotelListES> hotelListESArrayList = new ArrayList<>();
        for (HotelList hotelList : hotelLists) {
            HotelListES hotelListES = new HotelListES();
            hotelListES.setHotelSelectId(hotelList.getHotelSelectId());
            hotelListES.setHotelShowImg(hotelList.getHotelShowImg());
            hotelListES.setHotelId(hotelList.getHotelId());
            hotelListES.setHotelName(hotelList.getHotelName());
            hotelListES.setStarRanting(hotelList.getStarRanting());
            hotelListES.setRoomLeastPrice(hotelList.getRoomLeastPrice());
            hotelListES.setHotelMark(hotelList.getHotelMark());
            hotelListES.setVersion(hotelList.getVersion());
            hotelListES.setState(hotelList.getState());
            hotelListES.setCreateTime(hotelList.getCreateTime());
            hotelListES.setUpdateTime(hotelList.getUpdateTime());
            hotelListES.setStandby01(hotelList.getStandby01());
            hotelListES.setStandby02(hotelListES.getStandby02());
            hotelListESArrayList.add(hotelListES);
        }
        esHotelListService.saveAll(hotelListESArrayList);
        return "mysql同步至elasticsearch成功";
    }

    /**
     * elastic根据酒店名进行查询(测试阶段)
     */
    @GetMapping("/findByHotelName/{hotelName}/{pageNum}")
    public Page<HotelListES> findByHotelName(@PathVariable("hotelName") String hotelName,@PathVariable("pageNum") String pageNum){
        PageRequest page = PageRequest.of(Integer.parseInt(pageNum), 8);
        Page<HotelListES> hotelListES = esHotelListService.findByhotelName(hotelName,page);
        return hotelListES;
    }

    /**
     * elasticSearch无查询条件默认查询
     */
    @GetMapping("/findByPage/{pageNum}")
    public Page<HotelListES> findByPage(@PathVariable("pageNum") String pageNum){
        PageRequest page = PageRequest.of(Integer.parseInt(pageNum), 8);
        Page<HotelListES> hotelListES = esHotelListService.findAll(page);
        return hotelListES;
    }

    /**
     * elasticSearch统计分页查询总数据数量(无条件查询)
     * @return es查询出所有数据数量
     */
    @GetMapping("/esFindAllLength")
    public long findAllLength(){
        //数量统计
        long count = 0;
        //起始页
        int pageNumInt = 0;
        while (noEsHotelListService.selectByPageCount(pageNumInt)!=0){
            count += noEsHotelListService.selectByPageCount(pageNumInt);
            pageNumInt++;
        }
        System.out.println("分页查询总数据量:"+count);
        return count;
    }

    /**
     * elasticSearch统计分页查询总数据数量(按酒店名查询)
     * @param hotelName 酒店名
     * @return 统计数量
     */
    @GetMapping("/findAllLengthByName/{hotelName}")
    public long findAllLengthByName(@PathVariable("hotelName") String hotelName){
        //数量统计
        int count = 0;
        int pageNum = 0;
        Page<HotelListES> hotelListES = null;
        do{
            PageRequest page = PageRequest.of(pageNum , 8);
            hotelListES = esHotelListService.findByhotelName(hotelName,page);
            count += hotelListES.stream().count();
            pageNum ++;
        }while (hotelListES.stream().count()!=0);
        System.out.println("分页查询总数据量:"+count);
        return count;
    }
}
